package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@Api(tags = "菜品相关接口")
@RestController
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 新增菜品
     *
     * @param dishDTODTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result<String> save(@RequestBody DishDTO dishDTODTO) {
        log.info("新增菜品：{}", dishDTODTO);
        dishService.saveWithFlavor(dishDTODTO);

        // 清除缓存
        String cacheKey = "dish:category:" + dishDTODTO.getCategoryId();
        cleanCache(cacheKey);

        return Result.success();
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询：{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品
     *
     * @param ids 菜品id列表
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result<String> deleteById(@RequestParam List<Long> ids) {
        log.info("批量删除菜品列表：{}", ids);
        dishService.batchDeleteByIds(ids);

        // 清除缓存
        String cacheKey = "dish:category:*";
        cleanCache(cacheKey);

        return Result.success();
    }

    /**
     * 查询菜品详情
     *
     * @param id 菜品id
     * @return 菜品详情
     */
    @GetMapping("/{id}")
    @ApiOperation("查询菜品详情")
    public Result<DishVO> getDishById(@PathVariable Long id) {
        log.info("查询菜品详情：{}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * 更新菜品
     *
     * @param dishDTO 菜品信息
     * @return 是否成功
     */
    @PutMapping
    @ApiOperation("更新菜品")
    public Result<Boolean> update(@RequestBody DishDTO dishDTO) {
        log.info("更新菜品：{}", dishDTO);
        Assert.notNull(dishDTO.getId(), "菜品id不能为空");
        dishService.updateWithFlavor(dishDTO);

        // 清除缓存
        String cacheKey = "dish:category:*";
        cleanCache(cacheKey);

        return Result.success(true);
    }

    /**
     * 更新菜品状态
     *
     * @param id 菜品id
     * @param status 菜品状态
     * @return 是否成功
     */
    @PostMapping("/status/{status}")
    @ApiOperation("更新菜品状态")
    public Result<Boolean> updateStatus(@RequestParam Long id, @PathVariable Integer status) {
        log.info("更新菜品状态：id={}, status={}", id, status);
        dishService.updateStatus(id, status);

        // 清除缓存
        String cacheKey = "dish:category:*";
        cleanCache(cacheKey);

        return Result.success(true);
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId) {
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }

    /**
     * 清除缓存
     *
     * @param pattern 缓存key的模式
     */
    private void cleanCache(String pattern) {
        Set<String> keys = redisTemplate.keys(pattern);
        if (keys != null && !keys.isEmpty()) {
            redisTemplate.delete(keys);
        }
    }

}
